# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

class FT_0706:
    Alarm1=[
            [
            ["疲劳驾驶","正常","已疲劳状态","预留","无效值"],
            ["当天累计驾驶超时","正常","已超时","预留","无效值"],
            ["进出区域","正常","进入区域","驶出区域","无效值"],
            ["进出线路","正常","进入线路","驶出线路","无效值"]
            ],
            [
            ["路线偏离","正常","路线偏离","预留","无效值"],
            ["终端主电源状态","正常","主电源欠压","主电源掉电","无效值"],
            ["GNSS模块故障","正常","GNSS模块故障","预留","无效值"],
            ["GNSS天线状态","正常","GNSS天线开路","GNSS天线短路","无效值"]
            ],
            [
            ["车辆VSS故障","正常","VSS故障","预留","无效值"],
            ["车辆油量异常","正常","油量异常","预留","无效值"],
            ["车辆被盗(车辆防盗器)","正常","车辆被盗","预留","无效值"],
            ["车辆非法点火","正常","车辆非法点火","预留","无效值"]
            ],
            [
            ["车辆非法位移","正常","车辆非法位移","预留","无效值"],
            ["碰撞报警","正常","碰撞报警","预留","无效值"],
            ["侧翻报警","正常","侧翻报警","预留","无效值"],
            ["终端LCD或显示屏故障","正常","显示屏故障","预留","无效值"]
            ],
            [
            ["TTS模块故障","正常","TTS模块故障","预留","无效值"],
            ["6轴传感器","正常","6轴传感器异常","预留","无效值"],
            ["加密模块","正常","加密模块异常","预留","无效值",],
            ["司机卡模块状态","正常","模块异常","预留","无效值"]
            ],
            [
            ["USB导出接口","正常","接口异常","预留","无效值"],
            ["RS232导出接口","正常","接口异常","预留","无效值"],
            ["摄像头1状态","正常","摄像头1故障","预留","无效值"],
            ["摄像头2状态","正常","摄像头2故障","预留","无效值"]
            ],
            [
            ["摄像头3状态","正常","摄像头3故障","预留","无效值"],
            ["摄像头4状态","正常","摄像头4故障","预留","无效值"],
            ["摄像头5状态","正常","摄像头5故障","预留","无效值"],
            ["摄像头6状态","正常","摄像头6故障","预留","无效值"]
            ]
        ]
    Alarm2=[
            [
            ["燃料指示器1中的水","无水","含水","预留","无效值"],
            ["发动机黄色警告灯命令","灯灭，无严重故障","灯亮，严重故障","预留","无效值"],
            ["发动机红色警告灯命令","灯灭，无严重故障","灯亮，严重故障","预留","无效值"],
            ["OBD故障指示灯的命令","灯灭，无排放故障","灯亮，排放故障","预留","无效值"]
            ],
            [
            ["空滤阻塞报警","未阻塞","阻塞报警","预留","无效值"],
            ["水温高报警灯状态","正常","报警","预留","无效值"],
            ["刹车蹄片磨损报警","未磨损","磨损报警","故障","无效值"],
            ["驾驶室未锁止状态","锁止","未锁止","预留","无效值"],
            ],
            [
            ["制动灯故障(灯丝烧断)","正常","故障","预留","无效值"],
            ["气压过低报警状态","正常","报警","预留","无效值"],
            ["制动系统失效","正常","制动系统故障","预留","无效值"],
            ["真空度低报警","正常","报警","预留","无效值"],
            ],
            [
            ["驻车气路气压低报警开关","正常","报警","预留","无效值"],
            ["刹车片磨损","未磨损","磨损报警","故障","无效值"],
            ["货箱倾翻指示","未侧翻","侧翻","故障","无效值"],
            ["驾驶室锁止","锁止","未锁止","故障","无效值"],
            ],
            [
            ["轮间差速锁","未工作","工作","故障","无效值"],
            ["轴间差速锁","未工作","工作","故障","无效值"],
            ["取力器","未工作","工作","故障","无效值"],
            ["驻车制动(手刹)","松开","拉起","故障","无效值"],
            ],
            [
            ["随动转向锁止","未工作","工作","故障","无效值"],
            ["转向液位低","液位正常","液位低报警","故障","无效值"],
            ["空滤堵塞","未阻塞","阻塞报警","故障","无效值"],
            ["安全带报警","已系","未系","故障","无效值"],
            ],
            [
            ["制动/离合液位低","正常","液位低报警","预留","无效值"],
            ["安全带状态(是否系)","已系","未系","故障","无效值"],
            ["协议中","","","",""],
            ["发动机冷却液位低","液位正常","液位低报警","故障","无效值"]
            ]
        ]
    Switch1=[
            [
            ["ACC状态","正常","ACC_ON开","ACC_ON状态错误","无效值"],
            ["IG_ON状态","正常","IG_ON开","IG_ON状态错误","无效值"],
            ["定位状态","定位无效","定位有效","漂移","无效值"],
            ["车门锁","车门锁开","车门落锁","预留","无效值"]
            ],
            [
            ["CAN总线1状态","已连接","未连接","预留","无效值"],
            ["CAN总线2状态","已连接","未连接","预留","无效值"],
            ["CAN总线3状态","已连接","未连接","预留","无效值"],
            ["UART总线1状态","已连接","未连接","预留","无效值"]
            ],
            [
            ["UART总线2状态","已连接","未连接","预留","无效值"],
            ["RS485总线连接","已连接","未连接","预留","无效值"],
            ["终端网络链接状态","已联网","未联网","预留","无效值"],
            ["锁车状态","正常","发动机限转速","发动机限启动","无效值"]
            ]
        ]
    Switch2=[
            [
            ["近光灯输出状态(ICU)","关","开","故障","无效值"],
            ["左转向灯状态","关","开","预留","无效值"],
            ["前雾灯输出状态","关","开","预留","无效值"],
            ["门锁电机工作状态(开锁,闭锁)","闭合","锁止","解锁","无效值"]
            ],
            [
            ["手动变速箱倒档开关","非倒档","倒档","预留","无效值"],
            ["远光灯输出状态(ICU)","关","开","故障","无效值"],
            ["门碰开关/驾驶员门状态","关","开","预留","无效值"],
            ["后雾灯输出状态","关","开","预留","无效值"]
            ],
            [
            ["近光灯输出状态(BCM)","关","开","故障","无效值"],
            ["手刹开关状态","手刹未拉起","手刹拉起","故障","无效值"],
            ["制动开关状态","未踩下","踩下","故障","无效值"],
            ["离合开关状态","未踩下","踩下","故障","无效值"]
            ],
            [
            ["制动灯输出状态","关","开","故障","无效值"],
            ["右转向灯输出状态","关","开","故障","无效值"],
            ["左转向灯输出状态","关","开","故障","无效值"],
            ["远光灯输出状态(BCM)","关","开","故障","无效值"]
            ],
            [
            ["燃油加热指示","未工作","工作","故障","无效值"],
            ["倒档开关","非倒档","倒档","故障","无效值"],
            ["变速箱低挡","非低档","低档","故障","无效值"],
            ["变速箱空挡","非空档","空档","故障","无效值"]
            ],
            [
            ["ASR发动机控制激活","正常未起作用","ASR发动机控制起作用","故障","无效值"],
            ["ASR制动控制激活","正常未起作用","ASR制动控制起作用","故障","无效值"],
            ["ABS激活","正常未起作用","防抱死系统激活","故障","无效值"],
            ["EBS制动开关","正常未起作用","电子制动系统起作用","故障","无效值"]
            ],
            [
            ["右转向灯状态","关","开","预留","无效值"],
            ["挂车手制动开关","手刹未拉起","手刹拉起","故障","无效值"],
            ["辅助制动灯工作指示","未工作","工作","故障","无效值"],
            ["喇叭请求状态","关","开","预留","无效值"]
            ]
        ]
    Status2=[
            [
            ["后雾灯开关","关","开","未定义","无效值"],
            ["前雾灯开关","关","开","未定义","无效值"],
            ["近光灯开关","关","开","未定义","无效值"],
            ["远光灯开关","关","开","未定义","无效值"]
            ],
            [
            ["VDC完全工作指示","未完全","完全","未定义","无效值"],
            ["司机侧门窗开关信号","关","开","未定义","无效值"],
            ["危险报警灯开关","关","开","未定义","无效值"],
            ["驾驶员侧门碰开关","门关","门开","未定义","无效值"]
            ],
            [
            ["乘客侧门碰开关","门关","门开","未定义","无效值"],
            ["","","","",""],
            ["顶灯状态","关","开","未定义","无效值"],
            ["前雾灯状态","关","开","未定义","无效值"]
            ],
            [
            ["后雾灯状态","关","开","未定义","无效值"],
            ["安全带未系提醒","未系","已系","错误","无效值"],
            ["中控门锁开关","关","开","未定义","无效值"],
            ["司机侧右前门窗开关信号","关","开","未定义","无效值"]
            ]
            ]

    Speed = [["车速（发动机）:","4"], ["车速（仪表）:","4"], ["发动机燃油消耗率(平均):","4"], ["发动机瞬时油耗率（平均）:","4"], ["发动机转速:","4"], ["油门开度:","2"]]

    FuelFonsumption = [["平均油耗(仪表计算):","4"], ["NOx传感器浓度:","4"], ["剩余油量百分比（BCM）:","2"], ["剩余油量百分比:","2"], ["燃油液位:","2"], ["燃油液位(ICU):","2"],
            ["后桥气压传感器信号:","2"], ["前桥气压传感器信号:","2"], ["前桥气路气压:","2"],["后桥气路气压:","2"], ["发动机燃油消耗率:","4"], ["发动机瞬时消耗率:","4"], ["发动机平均油耗率:","4"], ["尿素液位(ECM):","2"], ["尿素箱温度(ECM):","2"],
            ["尿素液位(SCR):","2"], ["尿素箱温度(SCR):","2"], ["发动机燃油消耗率（累积）:","8"]]

    Power = [["发动机期望转速:","4"], ["发动机参考扭矩:","4"], ["发动机废气再循环1质量流率:","4"], ["发动机进气空气质量流量:","4"], ["发动机燃油轨压力命令:","4"],
            ["驾驶指令扭矩百分比:","2"], ["发动机实际转矩百分比:","2"], ["发动机实际转矩百分比（分数部分）:","2"],["发动机需求扭矩百分比:","2"], ["当前转速下负荷百分比:","2"], ["实际最大可用发动机转矩百分比:","2"], ["摩擦力扭矩百分比:","2"], ["巡航设定车速:","2"], ["节气门开度:","2"],
            ["传动轴目前档位:","2"], ["当前档位:","2"], ["前桥左轮速度:","4"],["前桥右轮速度:","4"], ["后桥左轮速度:","4"], ["后桥右轮速度:","4"]]

    Condition = [["机油温度:","4"], ["大气温度:","4"], ["排气温度:","4"], ["ECM输入电压:","4"], ["蓄电池电压:","4"], ["SCR后处理1进气温度:","4"],
            ["SCR后处理1出口温度:","4"], ["风扇转速:","4"], ["发动机冷却水温度:","2"],["燃油温度:","2"], ["发动机机油液位:","2"], ["机油压力:","2"], ["冷却剂液位:","2"], ["大气压力:","2"], ["增压器压力:","2"],
            ["进气歧管温度:","2"], ["进气压力信号:","2"], ["柴油机排气后处理液罐加热器:","2"],["发动机请求风扇1的转速:","2"], ["发动机降扭请求:","2"], ["NOx传感器浓度(上游):","4"],
            ["空气质量流量:","4"], ["DPF压差:","4"], ["胎温:","4"],["输出轴转速:","4"], ["缓速器实际降扭百分比_0F:","2"], ["缓速器实际降扭百分比_10:","2"], ["方向盘转角:","2"]]

    Accumulation = [["发动机累计运行时间:","8"], ["发动机累计转数:","8"], ["发动机累计怠速燃油消耗量:","8"], ["发动机累计怠速时间:","8"], ["发动机短程燃油消耗量:","8"], ["发动机累计燃油消耗量:","8"],
            ["高精度里程(VDR):","8"], ["高精度里程(ICU):","8"], ["高精度里程(ECM):","8"],["发动机运行总时间:","8"], ["低精度里程(ECM):","8"], ["整车重量:","8"], ["发动机从启动开始运行的时间:","4"]]

    Status1 = [["发动机扭矩模式:","2"], ["DPF热能管理活动:","2"], ["巡航控制激活状态标识:","2"], ["巡航使能开关:","2"], ["巡航设定开关:","2"], ["PTO状态:","2"], ["巡航控制状态:","2"], ["加速器连锁开关:","2"], ["风扇状态:","2"],
            ["发动机工作状态:","2"],["空调高压风机开关:","2"], ["右门锁反馈信号:","2"], ["左门锁电机工作状态（开锁，闭锁）:","2"], ["后处理1进气露点:","2"], ["后处理1烟气露点:","2"], ["雨刮档位:","2"], ["洗涤:","2"], ["左侧门窗升降状态:","2"],
            ["右侧门窗升降状态:","2"],["转向灯开关:","2"], ["遥控信号:","2"], ["遥控状态(开、闭、寻车):","2"], ["偏向控制发动机激活:","2"]]

    analysis0706 = ["自定义消息体长度:", "数据组个数:", "采集起始时间:", "数据块ID：", "数据块个数：", "偏移时间：", "数据项："]
    dataIDData = ["报警信息块 1:", "报警信息块 2:", "开关量信息块1:", "开关量信息块2：", "车速信息项定义：", "油耗信息块：",
                            "动力信息块：", "工况信息块：", "累积值信息块：", "状态1信息块：", "状态2信息块："]

    dataIDDataLength= [7, 7, 3, 7, 0, 0, 0, 0, 0, 0, 4]
    protocolStr =''
    messageLength=[]#自定义消息体长度
    dataSetNum=0#数据组个数
    startTime='' #采集起始时间
    dataID=0#数据ID
    dataNum=0#数据块个数
    offsetTime=''#偏移时间
    protocolData=''#数据项
    #ModelHex str = new ModelHex();
    databinary=[]#八段八bit数组（数据项列表）
    datalength =[]#ID对应的表使用的byte长度
    phase=''#相位图
    result = []
    def Init(self):
         self.setMessageLength()
         self.setDataSetNum()
         self.setStartTime()
         self.setDataID()
         self.setDataNum()

    def LogicAisle(self,protocol,header):
        self.result = header
        self.protocolStr = protocol
        print"Initstart"
        self.Init()
        print"Initend"
        self.result.append(self.analysis0706[0] + self.messageLength + '\n')
        self.result.append(self.analysis0706[1] + self.dataSetNum + '\n')
        self.result.append(self.analysis0706[2] + self.startTime + '\n')
        print int(self.dataID)
        self.result.append(self.analysis0706[3] + self.dataID +self.dataIDData[(int(self.dataID,16)-1)] + '\n')
        self.result.append(self.analysis0706[4] + self.dataNum + '\n')
        print"self.dataID=%s",self.dataID
        if (int(self.dataID,16) == 1 or int(self.dataID,16) == 2 or int(self.dataID,16) == 3 or int(self.dataID,16) == 4 or int(self.dataID,16) == 11):
            self.setOffsetTime()
            self.setProtocolData()
            self.result.append(self.analysis0706[5] + self.offsetTime + '\n')
            self.result.append(self.analysis0706[6] + self.protocolData + '\n')
            #self.databinary = str.hexString2binaryString(self.protocolData)
            self.databinary = bin(int(self.protocolData,16))
            self.databinary = self.databinary[2:]
            print"self.databinary_tpye",type(self.databinary)
            print"self.databinary=%s",self.databinary
            #for i in range(8):
                #for j in range(len(self.databinary)):
                    #k=j+2
                    #databinarylist={i,self.databinary[j:k]}
                    #print"i",databinarylist[i]
                    #databinarylist.append(self.databinary[i,j])
                    #i=j
            flag = self.protocolDataStr()
            temporary = self.AnalysisFT0706(self.databinary, self.dataIDDataLength[(int(self.dataID,16)-1)], flag)
            #temporary = self.AnalysisFT0706(databinarylist, self.dataIDDataLength[(int(self.dataID,16)-1)], flag)
            self.result.append("".join(temporary));
        else:
            self.setPhase()
            self.setoffsetTime()
            self.setprotocolData()
            self.result.append("数据项位图:" + self.phase + '\n')
            self.result.append(self.analysis0706[5] + self.offsetTime + '\n')
            self.result.append(self.analysis0706[6] + self.protocolData + '\n')
            flag = self.protocoldataStr()
            temporary = self.AnalysisNum(self.protocolData,flag)
            print"protocoldataStr_temporary",type(temporary)
            self.result.append("".join(temporary))

    def protocolDataStr(self):
        flag = []
        if(int(self.dataID,16) == 1):
            flag = self.Alarm1
        elif(int(self.dataID,16) == 2):
            flag = self.Alarm2
        elif(int(self.dataID,16) == 3):
            flag = self.Switch1
        elif(int(self.dataID,16) == 4):
            flag = self.Switch2
        elif(int(self.dataID,16) == 11):
            flag = self.Status2
        else:
            print"protocolDataStr"
        return flag

    def protocoldataStr(self):
        flag = []
        if (int(self.dataID,16) == 5):
            flag = self.Speed
        elif(int(self.dataID,16)== 6):
            flag = self.FuelFonsumption
        elif(int(self.dataID,16) == 7):
            flag = self.Power
        elif(int(self.dataID,16) == 8):
            flag = self.Condition
        elif(int(self.dataID,16) == 9):
            flag = self.Accumulation
        elif(int(self.dataID,16) == 10):
            flag = self.Status1
        else:
            print"protocoldataStr"
        return flag

    def setMessageLength(self):
        self.messageLength = self.protocolStr[0:4]
        print"setMessageLength"

    def setDataSetNum(self):
        #self.dataSetNum = Integer.parseInt(self.protocolStr[4:6], 16)
        self.dataSetNum =self.protocolStr[4:6]
        print"setDataSetNum"

    def setStartTime(self):
        self.startTime = self.protocolStr[6:22]
        print"setStartTime"

    def setDataID(self):
        #self.dataID = Integer.parseInt(self.protocolStr[22,24], 16)
        self.dataID =self.protocolStr[22:24]
        print"self.setDataID",self.dataID
    def setDataNum(self):
        #self.dataNum = Integer.parseInt(self.protocolStr[24,26], 16)
        self.dataNum =self.protocolStr[24:26]
        print"setDataNum"
    def setOffsetTime(self):
        self.offsetTime = self.protocolStr[26:30]
        print"setOffsetTime"
    def setProtocolData(self):
        self.protocolData = self.protocolStr[30:46]
        print"setProtocolData"

    def setPhase(self):
        self.phase = self.protocolStr[26:34]
        print"setPhase"
    def setoffsetTime(self):
        self.offsetTime = self.protocolStr[34:38]
        print"setoffsetTime"

    def setprotocolData(self):
        self.protocolData = self.protocolStr[38:(len(self.protocolStr)-4)]
        print"setprotocolData"

    def AnalysisFT0706(self, a,x,b):
        print"AnalysisFT0706"
        result=[]
        c=0
        for i in range(x):
            for j in range(4):
                #Tmp2Bit=a[i][2*j:2*j+2]
                t2b=a[c:(i+1)*8]
                c=(i+1)*8
                print "ttttttt",t2b
                Tmp2Bit=t2b[2*j:2*j+2]
                print("i=%d,j=%d,Tmp2Bit=%s",i,j,Tmp2Bit)
                if(Tmp2Bit=="00"):
                    result.append(b[i][j][0]+"  :00:  "+b[i][j][1]+'\n')
                if(Tmp2Bit=="01"):
                    result.append(b[i][j][0]+"  :01:  "+b[i][j][2]+'\n')
                if(Tmp2Bit=="10"):
                    result.append(b[i][j][0]+"  :10  "+b[i][j][3]+'\n')
                if(Tmp2Bit=="11"):
                    result.append(b[i][j][0]+"  :11  "+b[i][j][4]+'\n')
        return result
    def AnalysisNum(self,protocolData='',flag=[]):
        result=[]
        mark = 0
        print"flag",len(flag)
        for i in range(len(flag)):
            #result.append(flag[i][0] + protocolData[mark: (mark + Integer.parseInt(flag[i][1]))] + '\n')
            print"AnalysisNum",i
            print"flag0",flag[i][0]
            print"flag1",flag[i][1]
            print"mark",mark
            print"flagtype",type(flag[i][0])
            print"0protocolData[mark: (mark + int(flag[i][1],16))]",type(protocolData[mark: (mark + int(flag[i][1],16))])
            result.append(flag[i][0] + protocolData[mark: (mark + int(flag[i][1],16))] + '\n')
            print"1protocolData[mark: (mark + int(flag[i][1],16))]",type(protocolData[mark: (mark + int(flag[i][1],16))])
            #mark = mark + Integer.parseInt(flag[i][1])
            mark = mark + int(flag[i][1],16)
            print"AnalysisNum_result",result
            print"AnalysisNum_result_type",type(result)
        return result
